package com.example.boot.aspect;

import com.mongodb.BasicDBObject;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author canger
 * @Deseription 引用mongodb储存日志的切面
 * @create 2018/8/27 15:59
 **/
@Aspect
@Order(1)       //切面的优先级1-10
@Component
public class WebLogAspect {

    private  Logger logger=Logger.getLogger("mongodb");

    @Pointcut("execution(public * com.example.boot.collection.*.*(..))")
    public void webLog(){}

    @Before("webLog()")
    public  void doBegore(JoinPoint joinPoint){
        //获取HttpServletRequest
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        BasicDBObject dbObject = getBasicDBObject(request, joinPoint);
        logger.info(dbObject);

//        logger.info("访问URL:"+request.getRequestURL().toString());
//        logger.info("访问方法:"+request.getMethod());
//        logger.info("访问ip:"+request.getRemoteAddr());
//        //获取访问类的class和方法
//        logger.info("访问ip:"+joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
//        //获取访问时的参数和值
//        logger.info("args:"+ Arrays.toString(joinPoint.getArgs()));

    }
    private BasicDBObject getBasicDBObject(HttpServletRequest request, JoinPoint joinPoint){
        BasicDBObject basicDBObject = new BasicDBObject();
        basicDBObject.append("requestURL", request.getRequestURL().toString());
        basicDBObject.append("requestURI", request.getRequestURI());
        basicDBObject.append("queryString", request.getQueryString());
        basicDBObject.append("remoteAddr", request.getRemoteAddr());
        basicDBObject.append("remoteHost", request.getRemoteHost());
        basicDBObject.append("remotePort", request.getRemotePort());
        basicDBObject.append("localAddr", request.getLocalAddr());
        basicDBObject.append("localName", request.getLocalName());
        basicDBObject.append("method", request.getMethod());
        basicDBObject.append("headers", getHeadersInfo(request));
        basicDBObject.append("parameters", request.getParameterMap());
        basicDBObject.append("classMethod", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        basicDBObject.append("args", Arrays.toString(joinPoint.getArgs()));
        basicDBObject.append("datetime", System.currentTimeMillis());
        return basicDBObject;
    }
    private Map<String,String> getHeadersInfo(HttpServletRequest request){
        Map<String, String> map = new HashMap<String, String>();
        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String key = names.nextElement();
            String header = request.getHeader(key);
            map.put(key,header);
        }
        return map;
    }
}
